package admin

import (
	"duoke/model"
	"duoke/service"
	utils "duoke/utils/common"
	"duoke/utils/response"
	"duoke/utils/valid"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

var UserCarmine = new(userCarmineCtl)

type userCarmineCtl struct{}

func (ctl *userCarmineCtl) List(c *gin.Context) {
	var req model.SearchUserCarmine
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), "")
		return
	}
	list, err := service.UserCarmine.List(c, &req)
	if err != nil {
		response.Error(c, err.Error(), "")
		return
	}
	response.Success(c, "获取成功", list)
}
func (ctl *userCarmineCtl) Give(c *gin.Context) {
	var req model.SearchUserCarmine
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), "")
		return
	}
	err := service.UserCarmine.GiveCarmine(c, req.FromId, req.Number, req.UserId)
	if err != nil {
		response.Error(c, err.Error(), "")
		return
	}
	response.Success(c, "赠送成功", "")
}

// Bind 绑定用户卡密
func (ctl *userCarmineCtl) Bind(c *gin.Context) {
	var req model.SearchUserCarmine
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	err := service.UserCarmine.Bind(c, &model.ReqUserCarmine{UserId: req.UserId, Id: req.Id})
	if err != nil {
		response.Error(c, err.Error(), "")
		return
	}
	response.Success(c, "绑定成功", "")
}
func (ctl *userCarmineCtl) Import(c *gin.Context) {
	// 准备数据通道和等待组
	importer := utils.NewExcelImporter()
	importer.RegisterParser("user_carmine", service.UserCarmine.ParseImport)
	importer.RegisterHandler("user_carmine", service.UserCarmine.ProcessProduct)
	importer.HandleImport(c, "user_carmine")
}
